package com.punai.dataanalysis.epdatakb.service.NG001change1212;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.common.core.domain.BaseEntity;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisNdgclpdqkhzb;
import com.punai.dataanalysis.epdatakb.mapper.NG001change1212.YearGclPdC1212Mapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class NG001YearGclPdC1212Service {


    @Autowired
    private YearGclPdC1212Mapper yearGclPdC1212Mapper;

    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        Map<String,Object> params = new HashMap<>();
        params.put("fgs",dataAnalysisVo.getFgs());
        params.put("month1",dataAnalysisVo.isMonth1());

        List<DataAnalysisNdgclpdqkhzb> resList = yearGclPdC1212Mapper.getInfos(params);
        List<JtczResultBo> bnkjqrsr = yearGclPdC1212Mapper.bnkjqrsr();
        Map<String, BigDecimal> kjqrsrMap = bnkjqrsr.stream().filter(e -> StrUtil.isNotBlank(e.getText())).collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));

        List<DataAnalysisNdgclpdqkhzb> res = new ArrayList<>();
        if (CollUtil.isNotEmpty(resList)) {
            for (DataAnalysisNdgclpdqkhzb dataAnalysisNdgclpdqkhzb : resList) {
                String pc = dataAnalysisNdgclpdqkhzb.getPc();
                if (StrUtil.isEmpty(pc)) {
                    continue;
                }
                BigDecimal bigDecimal = kjqrsrMap.get(pc);
                dataAnalysisNdgclpdqkhzb.setBnkjqrsr(bigDecimal);
                res.add(dataAnalysisNdgclpdqkhzb);
            }
        }

        DataAnalysisNdgclpdqkhzb yjs = fzXj(res, "竣工已结算");
        yjs.setProName("竣工已结算工程小计");
        DataAnalysisNdgclpdqkhzb wjs = fzXj(res, "竣工未结算");
        wjs.setProName("竣工未结算工程小计");
        DataAnalysisNdgclpdqkhzb zjxm = fzXj(res, "在建项目");
        zjxm.setProName("在建项目工程小计");
        DataAnalysisNdgclpdqkhzb zj = zj(res);
        zj.setProName("合计");

        // 重组 按照项目部
        res = xmbFz(res);
        res.add(yjs);
        res.add(wjs);
        res.add(zjxm);
        res.add(zj);
        return res;
    }

    private List<DataAnalysisNdgclpdqkhzb> xmbFz(List<DataAnalysisNdgclpdqkhzb> res) {
        List<DataAnalysisNdgclpdqkhzb> newResList = new ArrayList<>();
        Map<String, List<DataAnalysisNdgclpdqkhzb>> xmbGroup = res.stream().filter(e -> StrUtil.isNotBlank(e.getXmb())).collect(Collectors.groupingBy(DataAnalysisNdgclpdqkhzb::getXmb));
        for (Map.Entry<String, List<DataAnalysisNdgclpdqkhzb>> entry : xmbGroup.entrySet()) {
            String key = entry.getKey();
            List<DataAnalysisNdgclpdqkhzb> value = entry.getValue();
            newResList.addAll(value);
            DataAnalysisNdgclpdqkhzb zj = zj(value);
            zj.setProName(key  + "小计");
            newResList.add(zj);
        }
        return newResList;
    }

    // 分组 小计
    //  已结算工程小计
    //  竣工未结算工程小计
    //  在建工程小计

    private DataAnalysisNdgclpdqkhzb fzXj(List<DataAnalysisNdgclpdqkhzb> resList,String proStatus) {
        List<DataAnalysisNdgclpdqkhzb> collect = resList.stream().filter(e -> StrUtil.equals(e.getProStatus(), proStatus)).collect(Collectors.toList());
        return zj(collect);
    }

    private DataAnalysisNdgclpdqkhzb zj(List<DataAnalysisNdgclpdqkhzb> resList) {
        DataAnalysisNdgclpdqkhzb zj = new DataAnalysisNdgclpdqkhzb();
        BigDecimal htTotal = BigDecimal.ZERO;
        BigDecimal yearWccz = BigDecimal.ZERO;
        BigDecimal yearWcczZyfb = BigDecimal.ZERO;
        BigDecimal yearWcczSkYbjs = BigDecimal.ZERO;
        BigDecimal yearWcczSkJyjs = BigDecimal.ZERO;
        BigDecimal yearWcczJgc = BigDecimal.ZERO;
        BigDecimal ljkgpdz = BigDecimal.ZERO;
        BigDecimal ljjdhpdz = BigDecimal.ZERO;
        BigDecimal klpdz = BigDecimal.ZERO;
        BigDecimal klpdzZyfb = BigDecimal.ZERO;
        BigDecimal klWcczSkYbjs = BigDecimal.ZERO;
        BigDecimal klWcczSkJyjs = BigDecimal.ZERO;
        BigDecimal klWcczJgc = BigDecimal.ZERO;
        BigDecimal jdBeforeKjQrsr = BigDecimal.ZERO;
        BigDecimal jdAfterKjQrsr = BigDecimal.ZERO;
        BigDecimal yearBeforeLjQrsr = BigDecimal.ZERO;
        BigDecimal bnkjqrsr = BigDecimal.ZERO;
        BigDecimal sygcl = BigDecimal.ZERO;
        for (DataAnalysisNdgclpdqkhzb gcl : resList) {
            htTotal = cn.hutool.core.util.NumberUtil.add(gcl.getHtTotal(),htTotal);
            yearWccz = cn.hutool.core.util.NumberUtil.add(gcl.getYearWccz(),yearWccz);
            yearWcczZyfb = cn.hutool.core.util.NumberUtil.add(gcl.getYearWcczZyfb(),yearWcczZyfb);
            yearWcczSkYbjs = cn.hutool.core.util.NumberUtil.add(gcl.getYearWcczSkYbjs(),yearWcczSkYbjs);
            yearWcczSkJyjs = cn.hutool.core.util.NumberUtil.add(gcl.getYearWcczSkJyjs(),yearWcczSkJyjs);
            yearWcczJgc = cn.hutool.core.util.NumberUtil.add(gcl.getYearWcczJgc(),yearWcczJgc);
            ljkgpdz = cn.hutool.core.util.NumberUtil.add(gcl.getLjkgpdz(),ljkgpdz);
            ljjdhpdz = cn.hutool.core.util.NumberUtil.add(gcl.getLjjdhpdz(),ljjdhpdz);
            klpdz = cn.hutool.core.util.NumberUtil.add(gcl.getKlpdz(),klpdz);
            klpdzZyfb = cn.hutool.core.util.NumberUtil.add(gcl.getKlpdzZyfb(),klpdzZyfb);
            klWcczSkYbjs = cn.hutool.core.util.NumberUtil.add(gcl.getKlWcczSkYbjs(),klWcczSkYbjs);
            klWcczSkJyjs = cn.hutool.core.util.NumberUtil.add(gcl.getKlWcczSkJyjs(),klWcczSkJyjs);
            klWcczJgc = cn.hutool.core.util.NumberUtil.add(gcl.getKlWcczJgc(),klWcczJgc);
            jdBeforeKjQrsr = cn.hutool.core.util.NumberUtil.add(gcl.getJdBeforeKjQrsr(),jdBeforeKjQrsr);
            jdAfterKjQrsr = cn.hutool.core.util.NumberUtil.add(gcl.getJdAfterKjQrsr(),jdAfterKjQrsr);
            yearBeforeLjQrsr = cn.hutool.core.util.NumberUtil.add(gcl.getYearBeforeLjQrsr(),yearBeforeLjQrsr);
            bnkjqrsr = cn.hutool.core.util.NumberUtil.add(gcl.getBnkjqrsr(),bnkjqrsr);
            sygcl = NumberUtil.add(gcl.getSygcl(),sygcl);
        }
        zj.setHtTotal(         htTotal );
        zj.setYearWccz(        yearWccz );
        zj.setYearWcczZyfb(    yearWcczZyfb );
        zj.setYearWcczSkYbjs(  yearWcczSkYbjs );
        zj.setYearWcczSkJyjs(  yearWcczSkJyjs );
        zj.setYearWcczJgc(     yearWcczJgc );
        zj.setLjkgpdz(         ljkgpdz );
        zj.setLjjdhpdz(        ljjdhpdz );
        zj.setKlpdz(           klpdz );
        zj.setKlpdzZyfb(       klpdzZyfb );
        zj.setKlWcczSkYbjs(    klWcczSkYbjs );
        zj.setKlWcczSkJyjs(    klWcczSkJyjs );
        zj.setKlWcczJgc(       klWcczJgc );
        zj.setJdBeforeKjQrsr(  jdBeforeKjQrsr );
        zj.setJdAfterKjQrsr(   jdAfterKjQrsr );
        zj.setYearBeforeLjQrsr(yearBeforeLjQrsr );
        zj.setBnkjqrsr(        bnkjqrsr );
        zj.setSygcl(           sygcl );
        zj.hb();
        return zj;
    }

}
